package com.mia.props.client.container.guilib;

import net.minecraft.client.renderer.Tessellator;

public class GuiGauge extends GuiElement implements IGuiRenderable {
   private int[] textureUVs;
   protected boolean horizontal = false;
   protected boolean topleftStart = true;
   protected boolean drawBackground = false;
   protected float fill = 0.0F;

   public GuiGauge(IExposedGui parent, int x, int y, int width, int height, int[] iconUVs, boolean horizontal, boolean topleftStart, boolean drawBackground) {
      super(parent, x, y, width, height);
      this.textureUVs = iconUVs;
      this.horizontal = horizontal;
      this.topleftStart = topleftStart;
      this.drawBackground = drawBackground;
   }

   public void setFill(float fill) {
      if ((double)fill > 1.0D) {
         fill = 1.0F;
      } else if (fill < 0.0F) {
         fill = 0.0F;
      }

      this.fill = fill;
   }

   public void render(int mouseX, int mouseY) {
      Tessellator tessellator = Tessellator.instance;
      float height_full;
      float width_full;
      float xPositionFullL;
      float xPositionFullR;
      float yPositionFullT;
      float yPositionFullB;
      float xTextureFullL;
      float xTextureFullR;
      float yTextureFullT;
      float yTextureFullB;
      if (this.drawBackground) {
         height_full = (float)this.height_element;
         width_full = (float)this.width_element;
         if (this.horizontal) {
            width_full -= width_full * this.fill;
         } else {
            height_full -= height_full * this.fill;
         }

         if (this.topleftStart) {
            xPositionFullL = (float)(this.posX() + this.width_element) - width_full;
            xPositionFullR = (float)(this.posX() + this.width_element);
            yPositionFullT = (float)(this.posY() + this.height_element) - height_full;
            yPositionFullB = (float)(this.posY() + this.height_element);
         } else {
            xPositionFullL = (float)this.posX();
            xPositionFullR = (float)this.posX() + width_full;
            yPositionFullT = (float)this.posY();
            yPositionFullB = (float)this.posY() + height_full;
         }

         if (this.topleftStart) {
            xTextureFullL = (float)(this.textureUVs[0] + this.width_element) - width_full;
            xTextureFullR = (float)(this.textureUVs[0] + this.width_element);
            yTextureFullT = (float)(this.textureUVs[1] + this.height_element) - height_full;
            yTextureFullB = (float)(this.textureUVs[1] + this.height_element);
         } else {
            xTextureFullL = (float)this.textureUVs[0];
            xTextureFullR = (float)this.textureUVs[0] + width_full;
            yTextureFullT = (float)this.textureUVs[1];
            yTextureFullB = (float)this.textureUVs[1] + height_full;
         }

         tessellator.startDrawingQuads();
         tessellator.addVertexWithUV((double)xPositionFullL, (double)yPositionFullB, (double)this.parent.getZLevel(), (double)(xTextureFullL * 0.00390625F), (double)(yTextureFullB * 0.00390625F));
         tessellator.addVertexWithUV((double)xPositionFullR, (double)yPositionFullB, (double)this.parent.getZLevel(), (double)(xTextureFullR * 0.00390625F), (double)(yTextureFullB * 0.00390625F));
         tessellator.addVertexWithUV((double)xPositionFullR, (double)yPositionFullT, (double)this.parent.getZLevel(), (double)(xTextureFullR * 0.00390625F), (double)(yTextureFullT * 0.00390625F));
         tessellator.addVertexWithUV((double)xPositionFullL, (double)yPositionFullT, (double)this.parent.getZLevel(), (double)(xTextureFullL * 0.00390625F), (double)(yTextureFullT * 0.00390625F));
         tessellator.draw();
      }

      height_full = (float)this.height_element;
      width_full = (float)this.width_element;
      if (this.horizontal) {
         width_full *= this.fill;
      } else {
         height_full *= this.fill;
      }

      if (this.topleftStart) {
         xPositionFullR = (float)this.posX() + width_full;
         xPositionFullL = (float)this.posX();
         yPositionFullB = (float)this.posY() + height_full;
         yPositionFullT = (float)this.posY();
      } else {
         xPositionFullR = (float)(this.posX() + this.width_element);
         xPositionFullL = xPositionFullR - width_full;
         yPositionFullB = (float)(this.posY() + this.height_element);
         yPositionFullT = yPositionFullB - height_full;
      }

      if (this.topleftStart) {
         xTextureFullR = (float)this.textureUVs[2] + width_full;
         xTextureFullL = (float)this.textureUVs[2];
         yTextureFullB = (float)this.textureUVs[3] + height_full;
         yTextureFullT = (float)this.textureUVs[3];
      } else {
         xTextureFullR = (float)(this.textureUVs[2] + this.width_element);
         xTextureFullL = xTextureFullR - width_full;
         yTextureFullB = (float)(this.textureUVs[3] + this.height_element);
         yTextureFullT = yTextureFullB - height_full;
      }

      tessellator.startDrawingQuads();
      tessellator.addVertexWithUV((double)xPositionFullL, (double)yPositionFullB, (double)this.parent.getZLevel(), (double)(xTextureFullL * 0.00390625F), (double)(yTextureFullB * 0.00390625F));
      tessellator.addVertexWithUV((double)xPositionFullR, (double)yPositionFullB, (double)this.parent.getZLevel(), (double)(xTextureFullR * 0.00390625F), (double)(yTextureFullB * 0.00390625F));
      tessellator.addVertexWithUV((double)xPositionFullR, (double)yPositionFullT, (double)this.parent.getZLevel(), (double)(xTextureFullR * 0.00390625F), (double)(yTextureFullT * 0.00390625F));
      tessellator.addVertexWithUV((double)xPositionFullL, (double)yPositionFullT, (double)this.parent.getZLevel(), (double)(xTextureFullL * 0.00390625F), (double)(yTextureFullT * 0.00390625F));
      tessellator.draw();
   }
}
